<script setup lang="ts">
import {computed, markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetTreemapDataApi} from "@/api";
import type {Treemap} from "@/interface";
import 'echarts-wordcloud'

const treemap = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const treemapData = ref<Treemap>({
  data: []
})
const level = computed(()=>[
  {
    itemStyle: {
      borderColor: '#777',
      borderWidth: 0,
      gapWidth: 1
    },
    upperLabel: {
      show: false
    }
  },
  {
    itemStyle: {
      borderColor: '#555',
      borderWidth: 5,
      gapWidth: 1
    },
    emphasis: {
      itemStyle: {
        borderColor: '#ddd'
      }
    }
  },
  {
    colorSaturation: [0.35, 0.5],
    itemStyle: {
      borderWidth: 5,
      gapWidth: 1,
      borderColorSaturation: 0.6
    }
  }
])
const init = async () => {
  const {data: res} = await useGetTreemapDataApi()
  treemapData.value = res
  myChart.value = markRaw(echarts.init(treemap.value));
  draw();
}
const draw = () => {
  option.value = {
    title: {
      text: '小说各类别数量',
      left: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    series:[
      {
        name: '类别',
        type: 'treemap',
        visibleMin: 300,
        label: {
          show: true,
          formatter: '{b}'
        },
        itemStyle: {
          borderColor: '#fff'
        },
        upperLabel: {
          show: true,
          height: 30
        },
        levels:level.value,
        data: treemapData.value.data
      }
    ]

  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="treemap" class="treemap">
  </div>
</template>

<style scoped>
.treemap {
  width: 100%;
  height: 100%;
}
</style>